package com.lzh.others.basejava.Reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 1.什么是反射？
 */
public class Main_01 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException {
//        int publicStaticField = User.publicStaticField;
//        System.out.println("publicStaticField = " + publicStaticField);
//        User.myPublicStaticMethod();
        User user = new User("lzh",20);
        System.out.println(user.name);
        user.myPublicMethod();

//        Class<User> userClass = User.class;

//        User user = new User("lzh", 20);
//        Class<? extends User> aClass = user.getClass();
        Class<?> aClass1 = Class.forName("com.lzh.others.basejava.Reflection.User");
        Field[] declaredFields = aClass1.getDeclaredFields();
        Annotation[] annotations = aClass1.getDeclaredAnnotations();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
            System.out.println(declaredField.getType());
            System.out.println(declaredField.get(null));
            if (declaredField.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation myAnnotation = declaredField.getAnnotation(MyAnnotation.class);
                System.out.println("myAnnotation = " + myAnnotation);
            }
            System.out.println("======================");
        }
        for (Annotation annotation : annotations) {
            System.out.println("annotation = " + annotation);
        }
    }
}
